package org.elsfs.cloud.common.mybatis.ext.sql.support.executor;

import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.mybatis.ext.db.GenericSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.SqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.builder.GenericSelectSql;

/**
 * 查询单条数据SQL执行器实现
 *
 * @param <R> 结果类型

 */
@RequiredArgsConstructor
public class QueryOneSqlExecutor<R> extends GenericSelectSql<QueryOneSqlExecutor<R>> implements SqlExecutor<R> {

    /**
     * 通用SQL执行器
     */
    private final GenericSqlExecutor genericSqlExecutor;
    /**
     * 结果类
     */
    private final Class<R> clazz;

    /**
     * 执行SQL
     *
     * @return 结果
     */
    @Override
    public R execute() {
        this.where().limit(1);
        return this.genericSqlExecutor.selectOne(
            this.buildSqlFragment(),
            this.getSqlParameterManager().getSqlParameters(),
            this.clazz
        );
    }
}
